package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：MJS
 * @date ：Created in 2021/5/27  11:26
 * @description：TODO
 */
@Service
public class AlipayServiceImpl implements AlipayService {
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private AlipayClient alipayClient;
    //支付宝支付
    @Override
    public String submit(Long orderId) {
        //1.支付表信息（所有支付的都用）返回的是支付表
        PaymentInfo paymentInfo=paymentService.insert(orderId, PaymentType.ALIPAY);
        //2.调用支付宝的统一收单下单并支付页面接口
        //支付宝的客户端
        //创建支付宝支付请求界面
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //公共参数
        //同步通知URL
        request.setReturnUrl(AlipayConfig.return_payment_url);
        //异步通知URL
        request.setNotifyUrl(AlipayConfig.notify_payment_url);
        Map<String,Object> param=new HashMap<>();
        param.put("out_trade_no",paymentInfo.getOutTradeNo());
        param.put("product_code","FAST_INSTANT_TRADE_PAY");
        param.put("total_amount",paymentInfo.getTotalAmount());
        param.put("subject",paymentInfo.getSubject());
        request.setBizContent(JSONObject.toJSONString(param));
        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println(response.getBody());
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }

        return response.getBody();
    }
    //退钱
    @Override
    public void refund(String outTradeNo) {
        PaymentInfo paymentInfo=paymentService.select(outTradeNo);
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        Map map=new HashMap();
        map.put("refund_amount",paymentInfo.getTotalAmount());
        map.put("out_trade_no",outTradeNo);
        request.setBizContent(JSONObject.toJSONString(map));
        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
